package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Person;
import com.itheima.info.manager.domain.Student;

public class StudentDao{
    static Student[] students = new Student[5];

    static {
        students[0] = new Student("s001", "张三", "23", "山西");
        students[1] = new Student("s002", "赵四", "24", "东北");
        students[2] = new Student("s003", "王五", "88", "北京");
    }

    public boolean addStudent(Student student) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (null == stu) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            students[index] = student;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudentById(String deleteId) {
        int index = getIndex(deleteId);
        students[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            if (null != students[i] && students[i].getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStudent) {
        int index = getIndex(updateId);
        students[index] = newStudent;
    }
}
